﻿using System.IO;
using System.Text.Encodings.Web;
using System.Text.Json;
using System.Text.Unicode;

namespace LS.Screening.UI.Common
{
    public class ClientConfig
    {
        static string FilePath
        {
            get
            {
                var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config", $"Config_{typeof(ClientConfig).FullName}.json");
                if (!Directory.Exists(Path.GetDirectoryName(filePath)))
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(filePath));
                }
                return filePath;
            }
        }

        /// <summary>
        /// 系统标题
        /// </summary>
        public string Title { get; set; } = "甲状腺和颈动脉三维重建与筛查随访系统-网络版";

        /// <summary>
        /// 业务服务器地地址
        /// </summary>
        public string ServerUrl { get; set; } = "https://localhost:5001";

        /// <summary>
        /// 主窗体
        /// </summary>
        public string MainWindow { get; set; } = "LS.Screening.UI.Main.Views.dll;LS.Screening.UI.Main.Views.MainView";

        /// <summary>
        /// 认证服务地址
        /// </summary>
        public string AuthServerUrl { get; set; } = "https://auth.lingshumed.com";

        /// <summary>
        /// 设备码:AET
        /// </summary>
        public string DeviceCode { get; set; }

        /// <summary>
        /// 系统服务代码
        /// </summary>
        public string ServiceCode { get; set; } = "LS.Screening.CS";

        /// <summary>
        /// 超声设备Dicom文件目录
        /// </summary>
        public string DicomFolder { get; set; }

        /// <summary>
        /// 超声设备Dicom上传服务地址
        /// </summary>
        public string DicomUploadUrl { get; set; }

        /// <summary>
        /// Dicom文件是否自动同步
        /// </summary>
        public bool? DicomFileAutoSync { get; set; }

        static ClientConfig _instance;

        static JsonSerializerOptions jsonOptions = new JsonSerializerOptions
        {
            Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
            WriteIndented = true,
            DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
        };

        public static ClientConfig Instance
        {
            get
            {
                if (_instance == null)
                {
                    try
                    {
                        using (var fs = new FileStream(FilePath, FileMode.OpenOrCreate))
                        {
                            _instance = JsonSerializer.Deserialize<ClientConfig>(fs, jsonOptions);
                        }
                    }
                    catch
                    {
                        _instance = new ClientConfig();
                        _instance.Save();
                    }
                }
                return _instance;
            }
        }

        public void Save()
        {
            using (var fs = new FileStream(FilePath, FileMode.OpenOrCreate))
            {
                var jsonData = JsonSerializer.SerializeToUtf8Bytes(this, jsonOptions);
                fs.Write(jsonData, 0, jsonData.Length);
                fs.Flush();
            }
        }
    }
}
